package com.zipou.myFax.mail;

import com.zipou.myFax.Exception.MailServiceException;

public class ImapCheckingThread extends Thread {

	private MailServiceImpl mail;

	public final static Integer MULTIPLICATOR = 1000;
	
	public ImapCheckingThread(MailServiceImpl mail) {
		super();
		this.mail = mail;
		mail.getMyfax().getLogger().info("Launching the Watching Thread");
	}

	public final void run() {
		while (!this.isInterrupted()) {
			try {
				mail.getMyfax().getLogger().info("Will be POPING each " + mail.getMyfax().getProperty("POP_TIME") + " seconds from now");
				this.sleep(Integer.parseInt(mail.getMyfax().getProperty("POP_TIME")) * ImapCheckingThread.MULTIPLICATOR);
				mail.checkMail();
			} catch (InterruptedException e) {
				mail.getMyfax().getLogger().severe(
						"Thread Termine... " + e.getMessage());
			} catch (MailServiceException e) {
				mail.getMyfax().getLogger().severe(
						"Unable to Check Mail :" + e.getMessage());
			} catch (NumberFormatException e) {
				mail.getMyfax().getLogger().severe(
						"Number Format Exception :" + e.getMessage());
			}
		}
	}

}
